﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using org.in2bits.MyXls;
using System.Data;
using System.Windows.Forms;

namespace DSLOrder
{
    internal class ExcelSave
	{
		public bool CreateExcel(string FileName, DataTable DataSource, string jkname)
		{
			bool flag = false;
			try
			{
				XlsDocument xlsDocument = new XlsDocument();
				Worksheet worksheet = xlsDocument.Workbook.Worksheets.Add(jkname);
				XF xF = xlsDocument.NewXF();
				xF.HorizontalAlignment = HorizontalAlignments.Centered;
				xF.Font.Height = 256;
				ColumnInfo columnInfo = new ColumnInfo(xlsDocument, worksheet);
				columnInfo.ColumnIndexStart = 0;
				columnInfo.ColumnIndexEnd = (ushort)(DataSource.Columns.Count - 2);
				columnInfo.Width = 4500;
				worksheet.AddColumnInfo(columnInfo);
				Cells cells = worksheet.Cells;
				for (int i = 0; i < DataSource.Columns.Count; i++)
				{
					cells.Add(1, i + 1, DataSource.Columns[i].ColumnName, xF);
				}
				for (int j = 0; j < DataSource.Rows.Count; j++)
				{
					for (int i = 0; i < DataSource.Columns.Count; i++)
					{
						cells.Add(j + 2, i + 1, DataSource.Rows[j][i].ToString());
					}
				}
				int num = FileName.LastIndexOf("\\");
				xlsDocument.FileName = FileName.Substring(num + 1, FileName.Length - (num + 1));
				string path = FileName.Substring(0, num + 1);
				xlsDocument.Save(path, overwrite: true);
				flag = true;
			}
			catch (Exception ex)
			{
				flag = false;
				MessageBox.Show(ex.Message);
			}
			return flag;
		}
	}
}

